package com.ruoyi.web.controller.market;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.*;
import com.ruoyi.market.service.*;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 订单管理控制器
 */
@Controller
@RequestMapping("/market/order")
public class OrderManagementController extends BaseController {

    private String prefix="market/order";

    @Autowired
    private RepoCkService2 repoCkService2;

    @Autowired
    private RepoWareService2 repoWareService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private RepoKcService2 repoKcService2;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrderDetailService detailService;

    @Autowired
    private PoProductService2 productService;

    @Autowired
    private SysProvinceService provinceService;

    @Autowired
    private SysCityService cityService;

    @RequiresPermissions("market:order:view")
    @GetMapping()
    public String customer(ModelMap mmap){
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/order";
    }

    /**
     * 查询客户管理信息列表
     */
    @RequiresPermissions("market:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order, ModelMap mmap)
    {
        SysUser user = getSysUser();
        mmap.put("user",user);
        startPage();
        List<Order> list = orderService.queryList(order);
        return getDataTable(list);
    }



    /**
     * 导出客户管理信息列表
     */
    @RequiresPermissions("market:order:export")
    @Log(title = "客户管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order)
    {
        List<Order> list = orderService.queryList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 客户管理信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/add";
    }

    /**
     * 新增保存客户管理信息
     */
    @RequiresPermissions("market:order:add")
    @Log(title = "新增订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Order2 order2)
    {
        System.out.println(order2);
        System.out.println(order2.getPoProduct2List().size());


        Order order=new Order();
        order.setOrderIndent(order2.getOrderIndent());
        order.setOrderTime(order2.getOrderTime());
        order.setSysOperUser(order2.getSysOperUser());
        order.setChkStatus(order2.getChkStatus());
        order.setOrderMoney(order2.getOrderMoney());
        SysUser user=getSysUser();
        Integer userid=user.getUserId().intValue();
        //将操作人注入order对象
        order.setSysOperUserId(userid);
        List<PoProduct2> poProduct2List=order2.getPoProduct2List();
        boolean flag=false;
        System.out.println(poProduct2List.size());
        List<OrderDetail> orderDetailList=new ArrayList<OrderDetail>();
        for (PoProduct2 poProduct2 : poProduct2List) {
            System.out.println(poProduct2);
            //封装查询商品id的3个条件
            PoProduct1 p=new PoProduct1();
            p.setPoBrandId(poProduct2.getPoBrandId());
            p.setPoTypeId(poProduct2.getPoTypeId());
            p.setProductModel(poProduct2.getProductModel());

            System.out.println(p.getPoBrandId());
            //根据商品的品牌类型型号查询商品对象
            PoProduct1 product = productService.findByBrandTypeModel(p);
        //获取客户id注入order对象
        order.setMkCustomerId(customerService.findByName(order2.getCustomerName()).getId());
        //获取仓库id注入order对象
        order.setRepoWareId(repoKcService2.findByPoProductId(product.getProductId()).getRepoWareId());

        //订购详情
        OrderDetail orderDetail=new OrderDetail();
        //获取订单号
        orderDetail.setMkOrderIndent(order.getOrderIndent());
        //商品id
        orderDetail.setPoProductId(product.getProductId());
        //获取商品数量
        orderDetail.setNum(poProduct2.getNum());
        //获取一种商品的总金额
        orderDetail.setTotal(poProduct2.getTotal());
        System.out.println("--------单个商品金额-----"+poProduct2.getTotal());
        //获取一种商品的单价
        orderDetail.setPrice(poProduct2.getPrice());
        //将详情对象装进集合
            orderDetailList.add(orderDetail);
        //新增订单详情
//            flag= detailService.save(orderDetail);
        }
        //新增订购单
        System.out.println(order);
        boolean flag1=orderService.addOrder(order,orderDetailList);
            return toAjax(flag1);
    }


    /**
     * 出库详情
     */
    @GetMapping("/detail2/{orderIndent}")
    public String detail2(@PathVariable("orderIndent") String orderIndent,ModelMap mmap) throws JsonProcessingException {
        //创建一个封装数据的对象
        Order2 order2=new Order2();
        System.out.println(orderIndent);
        //根据订单号查询订购单
        Order order = orderService.getByOrderIndent(orderIndent);
        //注入order数据
        order2.setCustomerName(customerService.findById(order.getMkCustomerId()).getName());
        order2.setOrderIndent(order.getOrderIndent());
        order2.setOrderTime(order.getOrderTime());
        order2.setSysOperUser(order.getSysOperUser());
        order2.setChkStatus(order.getChkStatus());
        order2.setOrderMoney(order.getOrderMoney());
        System.out.println(order);
        //根据订单号查询订购详情列表
        List<OrderDetail> detailList=orderDetailService.getDetailListBymkOrderIndent(orderIndent);
        //获取到商品详情列表
        List<PoProduct1> poProductList=new ArrayList<PoProduct1>();
        for (OrderDetail orderDetail : detailList) {
            System.out.println("数量"+orderDetail.getNum());
            PoProduct1 poProduct = productService.getById(orderDetail.getPoProductId());
            System.out.println(poProduct);
            poProductList.add(poProduct);
        }

        //把商品信息封装到PoProduct2集合中
        List<PoProduct2> poProduct2List=new ArrayList<PoProduct2>();

        Integer size=detailList.size();
        for (Integer i = 0; i < size; i++) {
            PoProduct2 poProduct2=new PoProduct2();
            poProduct2.setDetailId(detailList.get(i).getDetailId());
            poProduct2.setNum(detailList.get(i).getNum());
            poProduct2.setPrice(detailList.get(i).getPrice());
            poProduct2.setTotal(detailList.get(i).getTotal());

            poProduct2.setPoBrandId(poProductList.get(i).getPoBrandId());
            poProduct2.setPoTypeId(poProductList.get(i).getPoTypeId());
            poProduct2.setProductModel(poProductList.get(i).getProductModel());
            poProduct2.setUnit(poProductList.get(i).getUnit());

            poProduct2List.add(poProduct2);

            System.out.println("----3-----"+poProduct2);
        }

        /**
         * 获取出库详情
         */
        RepoCk2 repoCk2= repoCkService2.getByOrderIndent(orderIndent);

        //获取仓库名称
        RepoWare2 repoWare2=repoWareService.getByWareId(repoCk2.getRepoWareId());

        mmap.put("poProductList", poProductList);
        mmap.put("detailList", detailList);

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(poProduct2List);
        mmap.addAttribute("poProduct2List", jsonStr);
        mmap.put("repoWare2", repoWare2);
        mmap.put("repoCk2", repoCk2);
        mmap.put("order", order);
        mmap.put("order2", order2);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/detail2";
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail/{orderIndent}")
    public String detail(@PathVariable("orderIndent") String orderIndent,ModelMap mmap) throws JsonProcessingException {
        //创建一个封装数据的对象
        Order2 order2=new Order2();
        System.out.println(orderIndent);
        //根据订单号查询订购单
        Order order = orderService.getByOrderIndent(orderIndent);
        //注入order数据
        order2.setCustomerName(customerService.findById(order.getMkCustomerId()).getName());
        order2.setOrderIndent(order.getOrderIndent());
        order2.setOrderTime(order.getOrderTime());
        order2.setSysOperUser(order.getSysOperUser());
        order2.setChkStatus(order.getChkStatus());
        order2.setOrderMoney(order.getOrderMoney());
        System.out.println(order);
        //根据订单号查询订购详情列表
        List<OrderDetail> detailList=orderDetailService.getDetailListBymkOrderIndent(orderIndent);
        //获取到商品详情列表
        List<PoProduct1> poProductList=new ArrayList<PoProduct1>();
        for (OrderDetail orderDetail : detailList) {
            System.out.println("数量"+orderDetail.getNum());
            PoProduct1 poProduct = productService.getById(orderDetail.getPoProductId());
            System.out.println(poProduct);
            poProductList.add(poProduct);
        }

        //把商品信息封装到PoProduct2集合中
        List<PoProduct2> poProduct2List=new ArrayList<PoProduct2>();

        Integer size=detailList.size();
        for (Integer i = 0; i < size; i++) {
            PoProduct2 poProduct2=new PoProduct2();
            poProduct2.setDetailId(detailList.get(i).getDetailId());
            poProduct2.setNum(detailList.get(i).getNum());
            poProduct2.setPrice(detailList.get(i).getPrice());
            poProduct2.setTotal(detailList.get(i).getTotal());

            poProduct2.setPoBrandId(poProductList.get(i).getPoBrandId());
            poProduct2.setPoTypeId(poProductList.get(i).getPoTypeId());
            poProduct2.setProductModel(poProductList.get(i).getProductModel());
            poProduct2.setUnit(poProductList.get(i).getUnit());

            poProduct2List.add(poProduct2);

            System.out.println("----3-----"+poProduct2);
        }

        mmap.put("poProductList", poProductList);
        mmap.put("detailList", detailList);

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(poProduct2List);
        mmap.addAttribute("poProduct2List", jsonStr);
        mmap.put("order", order);
        mmap.put("order2", order2);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/detail";
    }


    /**
     * 提交审核跳转页面
     */
    @GetMapping("/assign/{orderIndent}")
    public String assign(@PathVariable("orderIndent") String orderIndent,ModelMap mmap)
    {
        Order order = orderService.getByOrderIndent(orderIndent);
        System.out.println("提交审核跳转页面"+order);
        mmap.put("order", order);
        return prefix + "/assign";
    }

    /**
     *提交审核
     */
    @RequiresPermissions("market:order:assign")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @PostMapping("/assign")
    @ResponseBody
    public AjaxResult assignSave(Order order) {
        return toAjax(orderService.modify(order));
    }

    /**
     * 修改保存客户管理信息
     */
    @RequiresPermissions("market:order:edit")
    @Log(title = "客户管理信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Order2 order2)
    {
        System.out.println("修改"+order2);
        System.out.println("修改"+order2.getPoProduct2List().size());
        Order order=new Order();
        order.setOrderIndent(order2.getOrderIndent());
        order.setOrderTime(order2.getOrderTime());
        order.setSysOperUser(order2.getSysOperUser());
        order.setChkStatus(order2.getChkStatus());
        order.setOrderMoney(order2.getOrderMoney());
        //获取客户id注入order对象
        order.setMkCustomerId(customerService.findByName(order2.getCustomerName()).getId());
        System.out.println(order);

        List<PoProduct2> poProduct2List=order2.getPoProduct2List();
        boolean flag=false;
        System.out.println(poProduct2List.size());
        for (PoProduct2 poProduct2 : poProduct2List) {
            System.out.println(poProduct2);
            //封装查询商品id的3个条件
            PoProduct1 p=new PoProduct1();
            p.setPoBrandId(poProduct2.getPoBrandId());
            p.setPoTypeId(poProduct2.getPoTypeId());
            p.setProductModel(poProduct2.getProductModel());
            System.out.println(p.getPoBrandId());

            //根据商品的品牌类型型号查询商品对象
            PoProduct1 product = productService.findByBrandTypeModel(p);

            //获取仓库id注入order对象
            order.setRepoWareId(repoKcService2.findByPoProductId(product.getProductId()).getRepoWareId());
            System.out.println(order);
            //订购详情
            OrderDetail orderDetail=new OrderDetail();


            //获取订购单号
            orderDetail.setMkOrderIndent(order.getOrderIndent());
            //商品id
            orderDetail.setPoProductId(product.getProductId());
            //获取商品数量
            orderDetail.setNum(poProduct2.getNum());
            //获取一种商品的总金额
            orderDetail.setTotal(poProduct2.getTotal());
            System.out.println("--------单个商品金额-----"+poProduct2.getTotal());
            //获取一种商品的单价
            orderDetail.setPrice(poProduct2.getPrice());
            //获取订单详情编号
            orderDetail.setDetailId(poProduct2.getDetailId());
            //修改订单详情
            flag= detailService.modify(orderDetail);
        }
        //修改订购单
        boolean flag1=orderService.modify(order);
        if(flag==true&&flag1==true){
            System.out.println("修改："+true);
            return toAjax(true);
        }else{
            System.out.println("修改："+false);
            return toAjax(false);
        }
    }


    /**
     * 修改订单信息
     */
    @GetMapping("/edit/{orderIndent}")
    public String edit(@PathVariable("orderIndent") String orderIndent,ModelMap mmap) throws JsonProcessingException {
        //创建一个封装数据的对象
        Order2 order2=new Order2();
        System.out.println(orderIndent);
        //根据订单号查询订购单
        Order order = orderService.getByOrderIndent(orderIndent);
        //注入order数据
        order2.setCustomerName(customerService.findById(order.getMkCustomerId()).getName());
        order2.setOrderIndent(order.getOrderIndent());
        order2.setOrderTime(order.getOrderTime());
        order2.setSysOperUser(order.getSysOperUser());
        order2.setChkStatus(order.getChkStatus());
        order2.setOrderMoney(order.getOrderMoney());
        System.out.println(order);
        //根据订单号查询订购详情列表
        List<OrderDetail> detailList=orderDetailService.getDetailListBymkOrderIndent(orderIndent);
        //获取到商品详情列表
        List<PoProduct1> poProductList=new ArrayList<PoProduct1>();
        for (OrderDetail orderDetail : detailList) {
            System.out.println("数量"+orderDetail.getNum());
            PoProduct1 poProduct = productService.getById(orderDetail.getPoProductId());
            System.out.println(poProduct);
            poProductList.add(poProduct);
        }

        //把商品信息封装到PoProduct2集合中
        List<PoProduct2> poProduct2List=new ArrayList<PoProduct2>();

        Integer size=detailList.size();
        for (Integer i = 0; i < size; i++) {
                PoProduct2 poProduct2=new PoProduct2();
                poProduct2.setDetailId(detailList.get(i).getDetailId());
                poProduct2.setNum(detailList.get(i).getNum());
                poProduct2.setPrice(detailList.get(i).getPrice());
                poProduct2.setTotal(detailList.get(i).getTotal());

                poProduct2.setPoBrandId(poProductList.get(i).getPoBrandId());
                poProduct2.setPoTypeId(poProductList.get(i).getPoTypeId());
                poProduct2.setProductModel(poProductList.get(i).getProductModel());
                poProduct2.setUnit(poProductList.get(i).getUnit());

                poProduct2List.add(poProduct2);

                System.out.println("----3-----"+poProduct2);
        }

        mmap.put("poProductList", poProductList);
        mmap.put("detailList", detailList);

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(poProduct2List);
        mmap.addAttribute("poProduct2List", jsonStr);
        mmap.put("order2", order2);
        SysUser user = getSysUser();
        mmap.put("user",user);
        return prefix + "/edit";
    }

    /**
     * 查询订单详情表
     * @return
     */
    @GetMapping("/getDetailList")
    @ResponseBody
    public List<PoProduct2> getDetailList(String orderIndent,ModelMap mmap){
        //创建一个封装数据的对象
        Order2 order2=new Order2();
        System.out.println("订单号："+orderIndent);
        //根据订单号查询订购单
        Order order = orderService.getByOrderIndent(orderIndent);
        //注入order数据
        order2.setOrderIndent(order.getOrderIndent());
        order2.setOrderTime(order.getOrderTime());
        order2.setSysOperUser(order.getSysOperUser());
        order2.setChkStatus(order.getChkStatus());
        order2.setOrderMoney(order.getOrderMoney());

        System.out.println("---------"+order);
        //根据订单号查询订购详情列表
        List<OrderDetail> detailList=orderDetailService.getDetailListBymkOrderIndent(orderIndent);
        //获取到商品详情列表
        List<PoProduct1> poProductList=new ArrayList<PoProduct1>();
        for (OrderDetail orderDetail : detailList) {
            System.out.println("-----------------数量"+orderDetail.getNum());
            PoProduct1 poProduct = productService.getById(orderDetail.getPoProductId());
            System.out.println(poProduct);
            poProductList.add(poProduct);
        }

        List<PoProduct2> poProduct2List=new ArrayList<PoProduct2>();
        PoProduct2 poProduct2=new PoProduct2();
        for (OrderDetail orderDetail : detailList) {
            poProduct2.setNum(orderDetail.getNum());
            poProduct2.setPrice(orderDetail.getPrice());
            poProduct2.setTotal(orderDetail.getTotal());
            System.out.println("----1-----"+poProduct2);
        }
        for (PoProduct1 poProduct : poProductList) {
            poProduct2.setPoBrandId(poProduct.getPoBrandId());
            poProduct2.setPoTypeId(poProduct.getPoTypeId());
            poProduct2.setProductModel(poProduct.getProductModel());
            poProduct2.setUnit(poProduct.getUnit());
            System.out.println("-----2----"+poProduct2);

        }
        poProduct2List.add(poProduct2);
        mmap.put("poProductList", poProductList);
        mmap.put("detailList", detailList);
        mmap.put("poProduct2List", poProduct2List);
        mmap.put("order", order);
        SysUser user = getSysUser();
        mmap.put("user",user);
        List<PoProduct2> date=new ArrayList<PoProduct2>();
        date=poProduct2List;
        for (PoProduct2 poProduct22 : poProduct2List) {
            System.out.println("----***********---"+poProduct22);
        }

        return date;
    }




    /**
     * 删除客户管理信息
     */
    @RequiresPermissions("market:order:remove")
    @Log(title = "客户管理信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String orderIndent)
    {
        System.out.println(orderIndent);
        //删除订单表和订单详情多表
        boolean a = false;
            a = orderService.deleteOrder(orderIndent);
            return toAjax(a);
    }
}
